


function createStore(reducer, preloadedState) {
    let state = preloadedState;
    let listenFns = []

    function getState() {
        return state
    }

    function subscribe(listenFn) {
        listenFns.push(listenFn)
        return () => {
            listenFns = listenFns.filter(fn => fn !== listenFn)
        }
    }

    function dispatch(action) {
        state = reducer(state, action)
        listenFns.forEach(listenFn => listenFn())
        return action
    }

    dispatch({ type: '@@REDUX/INIT' }) // 默认调用一次reducer 赋默认值
    return {
        getState,
        subscribe,
        dispatch
    }

}


export default createStore